import { defineStore } from 'pinia';
import { getLeftData } from '@/api/earlyWarning';
import { useCesiumPoints } from '@/hooks/useCesiumPoints';
import { getListByDate } from '@/api/thirdPartyRisk';
import COMPONENT_TITLES from '@/constants/components/componentsTitle';
import { useDialogStore } from '@/store/modules/useDialogStore'; // Cesium地图点相关hook

interface SeriesItem {
  name?: string;
  type?: string;
  data: number[];
  barWidth?: string;
  itemStyle?: any;
  label?: any;

  [key: string]: any;
}

export interface EChartsData {
  xaxisList: {
    data: string[];
  }[];
  seriesList: SeriesItem[];
}

export const useThirdPartyRiskStore = defineStore('thirdPartyRisk', () => {
  const echartsData = ref<EChartsData>(null);
  const dataSources = reactive<Record<string, any[]>>({});
  const { addPoint, clearAllPoints } = useCesiumPoints();
  const { flyTo } = useDialogStore();
  const getThirdPartyRisk = async () => {
    const { data } = await getLeftData({ type: 'thirdConstruction' });
    echartsData.value = data.thirdConstructionAlarmVo;
  };
  // 点击第三方回调
  const thirdPartyClick = async (item: any) => {
    flyTo({ longitude: 117.140035075, latitude: 36.798747059 }, 400000);
    if (dataSources[item.name]) {
      clearAllPoints(dataSources[item.name]);
      delete dataSources[item.name];
    } else {
      const { data } = await getListByDate({ time: item.name });
      dataSources[item.name] = data.map((point) => addPoint(point, COMPONENT_TITLES.THIRD_PARTY_CONSTRUCTION_MONITORING));
    }
  };
  //初始化点位(清除所有)
  const initThirdPartyRisk = () => {
    for (const key in dataSources) {
      clearAllPoints(dataSources[key]);
    }
  };
  return {
    echartsData,
    initThirdPartyRisk,
    thirdPartyClick,
    getThirdPartyRisk
  };
});
